<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-12-17
 * Time: 15:44
 */

namespace app\common\model;

use app\user\model\User;
use think\Db;
use think\Model as ThinkModel;

//银行列表模型
class Room extends ThinkModel
{
    protected $name = 'qt_room';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    public function roomlist($type,$page,$limit,$keyword,$store_id){
        $where=[];
        if(!empty($type)){
            $where[]=['type_id','eq',$type];
        }
        if(!empty($keyword)){
            $where[]=['name','like','%'.$keyword.'%'];
        }

        $status=Roomstatus::where('store_id',$store_id)->column('id,name');
        $color=Roomstatus::where('store_id',$store_id)->column('id,color');
        $list=$this
            ->where($where)
            ->page($page,$limit)
            ->select()
            ->each(function ($item)use($status,$store_id,$color){
                $item['status_name']=$status[$item['status_id']];
                $item['color']=$color[$item['status_id']];
                $userId = Orderqueue::where('store_id',$store_id)
                    ->where('room_id',$item['id'])
                    ->value('user_id');
                $item['engineer_id'] = Users::where('id',$userId)->column('work_number');
                return $item;

            });
        return $list;

    }
    public function roominfo($room_id,$store_id,$orderId){
        $status=['1'=>'空闲中','2'=>'服务中','3'=>'已结束','4'=>'换技师'];
        $info=$this
            ->where('id',$room_id)
            ->where('store_id',$store_id)
            ->find();

        $info['project']=Orderproject::where('room_id',$room_id)
            ->where('id',$orderId)
            ->where('status','<=','3')
            //->whereTime('created_time','today')
            ->select()
        ->each(function ($item)use($status){
            $item['created_time']=ST($item['created_time']);
            $item['work_number']=Users::where('id',$item['engineer_id'])->value('work_number');
            $item['status_name']=$status[$item['status']];
            return $item;
        });
        return $info;
    }

    /**
     * 根据房间ID获取项目商品账单详情
     * @param $roomId [房间ID]
     * @return array
     */
    public function getProductData($roomId,$where = []){
        if(empty($where)){
            $where = 1;
        }
        //项目
        $project = Db::name('qt_order')
            ->field('o.id,r.name as room_name,h.name as hand_card_name')
            ->alias('o')
            ->leftJoin('qt_room r', 'r.id = o.room_id')
            ->leftJoin('qt_hand_card h', 'h.id = o.hand_card_id')
            ->where(['room_id' => $roomId, 'pay_status' => '1'])
            ->where($where)
            ->order('o.id desc')
            ->find();
        $projectRes = Db::name('qt_order_project')
            ->field('id as product_order_id,name as goods_name,old_name as old_goods_name,order_type,created_time,
                engineer_id,start_time,end_time,real_end_time,old_card_id,hand_card_id,old_card_id,old_room_id,is_change,status,is_add,
                created_order_id as create_id,commission,receiver,sale_id,price,pay_status')
            ->where('order_id', $project['id'])
            ->where('is_delete',2)
            ->whereNotIn('status', ['4', '5'])
            ->whereNotNull('engineer_id')
            ->select();

        foreach ($projectRes as $key => $value) {
            $projectRes[$key]['work_number'] = Db::name('qt_users')->where('id', $value['engineer_id'])->value('work_number');//技师
            $projectRes[$key]['sale_name'] = Db::name('qt_users')->where('id', $value['sale_id'])->value('real_name');//销售人
            $projectRes[$key]['room_name'] = $project['room_name'];
            $projectRes[$key]['hand_card_name'] = Db::name('qt_hand_card')->where('id', $value['hand_card_id'])->value('name');
            $projectRes[$key]['old_card_name'] = Db::name('qt_hand_card')->where('id', $value['old_card_id'])->value('name');
            $projectRes[$key]['old_room'] = Db::name('qt_room')->where('id', $value['old_room_id'])->value('name');//原房间
            $projectRes[$key]['create_name'] = Db::name('qt_users')->where('id', $value['create_id'])->value('real_name');//下单人
            $projectRes[$key]['type'] = '1';//下单人
            $projectRes[$key]['goods_num'] = '1';//数量
        }

        //两个表的pay_status 定义不一样
        $order_where = $where;
        if ($order_where['pay_status'] == 1) {
            $order_where['pay_status'] = 0;
        }
        if ($order_where['pay_status'] == 2) {
            $order_where['pay_status'] = 1;
        }
        //商品
        $goods = Db::name('qt_product_order')
            ->field('o.id,r.name as room_name,h.name as hand_card_name,engineer_id')
            ->alias('o')
            ->leftJoin('qt_room r', 'r.id = o.room_id')
            ->leftJoin('qt_hand_card h', 'h.id = o.hand_card_id')
            ->where(['room_id' => $roomId])
            ->where($order_where)
            ->order('o.id desc')
            ->find();
        $productRes = Db::name('qt_product_order_item')
            ->field('id as product_order_id,goods_name,old_goods_name,old_goods_name,goods_num,create_time,
                create_id,sale_id,old_room_id,commission,receiver,goods_price as price,business_type,pay_status,hand_card_id')
            ->where('order_id', $goods['id'])
            ->where(['status'=>'1'])
            ->select();

        foreach ($productRes as $key => $value) {
            $productRes[$key]['engineer_name'] = '';//提成
            $productRes[$key]['sale_name'] = Db::name('qt_users')->where('id', $value['sale_id'])->value('real_name');//销售人
            $productRes[$key]['room_name'] = $goods['room_name'];
            $productRes[$key]['hand_card_name'] = Db::name('qt_hand_card')->where('id', $value['hand_card_id'])->value('name');//手牌;
            $productRes[$key]['create_name'] = Db::name('qt_users')->where('id', $value['create_id'])->value('real_name');//提成
            $productRes[$key]['old_card_name'] = '';
            $productRes[$key]['status'] = 0;
            $productRes[$key]['is_change'] = '1';
            $productRes[$key]['is_add_work'] = 0;
            $productRes[$key]['order_type'] = '';
            $productRes[$key]['start_time'] = null;
            $productRes[$key]['end_time'] = null;
            $productRes[$key]['real_end_time'] = null;
            $productRes[$key]['old_room'] = Db::name('qt_room')->where('id', $value['old_room_id'])->value('name');//原房间
            $productRes[$key]['type'] = '2';//商品
        }
        $data = array_merge($projectRes, $productRes);
        return $data;
    }


    /**
     * 根据房间ID获取房间预约信息
     * @param $roomId [房间ID]
     * @return array
     */
    public function getAppointment($roomId){
        $data = Db::name('qt_room_appointment')
            ->field('ra.id,p.name as project_name,ra.time,ra.delay,ra.content,u.real_name')
            ->alias('ra')
            ->leftJoin('qt_room r','r.id = ra.room_id')
            ->leftJoin('qt_project p','ra.project_id = p.id')
            ->leftJoin('qt_users u','ra.engineer_id = u.id')
            ->where(['ra.room_id'=>$roomId])
            ->select();
        return $data;

    }

    /**
     * 根据房间ID获取房间开房时间
     * @param $roomId [房间ID]
     * @return array
     */
    public function getOpen($roomId){
        $data = Db::name('qt_room_log')
            ->field('u.real_name,log.created_time')
            ->alias('log')
            ->leftJoin('qt_room r','r.id = log.room_id')
            ->leftJoin('qt_users u','u.id = log.user_id')
            ->where(['log.room_id'=>$roomId])
            ->order('log.id desc')
            ->find();
        return $data;

    }


    /**
     * 检测房间是否存在
     * @param $storeId
     * @param $name
     * @return bool
     */
    public static function checkRoomExist($storeId,$name,$id = ''){
        $find = [];
        if($id = ''){
            $find = self::where(['store_id'=>$storeId,'name'=>$name])->find();
        }else{
            $find = self::where(['store_id'=>$storeId,'name'=>$name])->where('id','neq',$id)->find();

        }
        return $find;

    }

    /**
     * 插入开房记录
     * @param $roomId
     * @param $userId
     */
    public static function openLog($roomId,$userId){
        $roomLog['room_id'] = $roomId;
        $roomLog['user_id'] = $userId;
        $roomLog['status'] = '2';
        $roomLog['created_time'] = time();
        Db::name('qt_room_log')->insert($roomLog);
    }




}